package com.itaem.community.modules.repair.controller;



import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.modules.repair.dto.RepairDto;
import com.itaem.community.modules.repair.entity.PicturesEntity;
import com.itaem.community.modules.repair.entity.RepairEntity;
import com.itaem.community.modules.repair.service.impl.PicturesServiceImpl;
import com.itaem.community.modules.repair.service.impl.RepairServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * <p>
 * 报修 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "报修模块")
@ResponseResult
@RestController
@RequestMapping("/repair")
public class RepairController {

    @Autowired
    private RepairServiceImpl repairService;
    @Autowired
    private PicturesServiceImpl picturesService;

    /**
     *
     * @param page 当前显示的是第page页
     * @param size 每页有size数据
     * @return 分页对象
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","维修工"},logical = Logical.OR)
    @ApiOperation("分页查询信息")
    @GetMapping("/{page}/{size}")
    public Page list(@PathVariable Long page, @PathVariable Long size){
        return repairService.page(new Page<RepairEntity>(page,size));
    }

    /**
     *
     * @param id 想要获取的信息的id
     * @return 获取的信息
     */
//    @RequiresRoles(value = {"超级管理员","物业工作人员","业主","维修工"},logical = Logical.OR)
    @ApiOperation("获取单条信息")
    @GetMapping("/{id}")
    public RepairEntity info(@PathVariable Long id){

        return repairService.getById(id);
    }

    /**
     *
     * @param repairDto 前端需要传入的参数
     * @return
     */
//    @RequiresRoles(value = {"超级管理员","业主"},logical = Logical.OR)
    @ApiOperation("新增")
    @PostMapping
    public Boolean save(@RequestBody RepairDto repairDto) {
        //报修
        RepairEntity repairEntity = new RepairEntity();
        BeanUtils.copyProperties(repairDto,repairEntity);
        repairEntity.setDeleted(false);
        repairEntity.setReportTime(LocalDateTime.now());
        repairService.save(repairEntity);
        return true;
    }
    /**
     *
     * @param repairDto 前端需要传入的参数
     * @return
     */
//    @RequiresRoles(value = {"超级管理员","业主"},logical = Logical.OR)
    @ApiOperation("更新")
    @PatchMapping
    public Boolean update(@RequestBody RepairDto repairDto) {
        RepairEntity repairEntity = new RepairEntity();
        BeanUtils.copyProperties(repairDto,repairEntity);
        repairService.update(repairEntity,new UpdateWrapper<RepairEntity>().eq("id",repairDto.id));
        return true;
    }

    /**
     *
     * @param id 需要删除的信息的id
     * @return
     */
//    @RequiresRoles(value = {"超级管理员","业主"},logical = Logical.OR)
    @ApiOperation("订单删除")
    @DeleteMapping("/{id}")
    public Boolean remove(@PathVariable Long id) {
        repairService.update(new UpdateWrapper<RepairEntity>().set("deleted",true).eq("id",id));
        picturesService.update(new UpdateWrapper<PicturesEntity>()
                .lambda()
                .eq(PicturesEntity::getRepairId,id)
                .set(PicturesEntity::getDeleted,true));
        return true;
    }

}

